/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {number[]}
 */
// 用数组保存遍历的链表值，再反转过来
var reversePrint = function (head) {
    const arr = [];
    let cur = head;
    while (cur) {
        arr.push(cur.val);
        cur = cur.next;
    }
    const res = [];
    const len = arr.length;
    for (let i = 0; i < len; i++) {
        res.push(arr.pop());
    }
    return res;
};
// 用数组保存遍历的链表值，每次从头部添加
var reversePrint = function (head) {
    const arr = [];
    let cur = head;
    while (cur) {
        arr.unshift(cur.val);
        cur = cur.next;
    }
    return arr;
};
// 先反转链表，再用数组保存遍历的链表值
var reversePrint = function (head) {
    let cur = head;
    let pre = undefined;
    let next = cur?.next
    while (cur) {
        cur.next = pre;
        pre = cur;
        cur = next;
        next = next?.next;
    }
    const arr = [];
    while (pre) {
        arr.push(pre.val);
        pre = pre.next;
    }
    return arr;
};

Xxt6802339